package com.bw.z.mvp.view.people;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.z.R;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.view.BaseFragment;
import com.bw.z.mvp.contract.NewsContract;
import com.bw.z.mvp.di.DaggerNewsComponent;
import com.bw.z.mvp.di.NewsModules;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.FollowEntity;
import com.bw.z.mvp.model.entity.UserFollowEntity;
import com.bw.z.mvp.presenter.NewsPresenter;
import com.bw.z.mvp.view.people.adapter.FollowNewsAdapter;
import com.bw.z.mvp.view.people.adapter.UserFollowAdapter;
import com.bw.z.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AttentionFragment extends BaseFragment<NewsPresenter>
        implements NewsContract.NewView, OnRefreshLoadMoreListener, OnItemClickListener {


    private SmartRefreshLayout attentionSmart;
    private RecyclerView attentionNewsRv;
    private View headView;
    private RecyclerView follow_author_rv;
    private LinearLayoutManager author, news;
    private FollowNewsAdapter followNewsAdapter;
    private UserFollowAdapter userFollowAdapter;
    private List<FollowEntity.Values> list = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.fragment_attention;
    }

    @Override
    public void initView() {
        attentionSmart = (SmartRefreshLayout) f(R.id.attention_smart);
        attentionNewsRv = (RecyclerView) f(R.id.attention_news_rv);

        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_follow_head, null);
        follow_author_rv = headView.findViewById(R.id.follow_author_rv);
        attentionSmart.setEnableLoadMore(true);
        attentionSmart.setEnableRefresh(true);
        attentionSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        attentionSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        attentionSmart.setOnRefreshLoadMoreListener(this);
        news = new LinearLayoutManager(getContext());
        news.setOrientation(LinearLayoutManager.VERTICAL);
        attentionNewsRv.setLayoutManager(news);
        author = new LinearLayoutManager(getContext());
        author.setOrientation(LinearLayoutManager.HORIZONTAL);
        follow_author_rv.setLayoutManager(author);
        userFollowAdapter = new UserFollowAdapter();
        followNewsAdapter = new FollowNewsAdapter();
        followNewsAdapter.addHeaderView(headView);
        attentionNewsRv.setAdapter(followNewsAdapter);
        follow_author_rv.setAdapter(userFollowAdapter);
        userFollowAdapter.setOnItemClickListener(this::onItemClick);
    }

    @Override
    public void inject() {
        DaggerNewsComponent.builder().newsModules(new NewsModules(this)).build().injectAttentionNews(this);
    }

    @Override
    public void initData() {
//        UserTable entity = App.getInstance().getDaoSession().queryBuilder(UserTable.class).list().get(0);
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
        newsMap.put("user_id", 1);
        Map<String, Object> followMap = new HashMap<>();
        followMap.put("code", HttpCode.USERFOLLOWCODE);
        followMap.put("user_id", 1);
        p.requestAll(followMap, newsMap);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        if (baseEntity instanceof FollowEntity) {
            FollowEntity followEntity = (FollowEntity) baseEntity;
            followNewsAdapter.setNewInstance(followEntity.getValues());
        } else {
            UserFollowEntity userFollowEntity = (UserFollowEntity) baseEntity;
            userFollowAdapter.setNewInstance(userFollowEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        FollowEntity followEntity = (FollowEntity) baseEntity;
        if (followEntity.getValues() == null) {
            ToastUtils.showMsg("刷新失败");
            return;
        }
        followNewsAdapter.setNewInstance(followEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        FollowEntity followEntity = (FollowEntity) baseEntity;
        if (followEntity.getValues() == null) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        if (followEntity.getValues().size() == 0) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        list = followNewsAdapter.getData();
        list.addAll(followEntity.getValues());
        followNewsAdapter.setNewInstance(list);
//        followNewsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
        p.requestLoad(newsMap);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        if (list != null)
            list.clear();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
        p.requestRefresh(newsMap);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        startPage(AuthorActivity.class);
    }
}